//
// Copyright (C) 2011 - 2013  Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.2 with QuantLib 1.2.1
//#include "stdafx.h"
#include "GeometricBrownianMotionProcess.h"
using namespace Cephei::QL::Processes;
#include <gen/QL/Math/Array.h>
#include <gen/QL/StochasticProcess1D.h>
using namespace Cephei::QL::Math;
using namespace Cephei::QL;
#define HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Processes::CGeometricBrownianMotionProcess::CGeometricBrownianMotionProcess (Double initialValue, Double mue, Double sigma) : CStochasticProcess1D(CGeometricBrownianMotionProcess::typeid)
{
    try
    {
#ifdef HANDLE
        _phGeometricBrownianMotionProcess = NULL;
#endif
        double _initialValue = (double)ValueHelper::Convert (initialValue); //d
        double _mue = (double)ValueHelper::Convert (mue); //d
        double _sigma = (double)ValueHelper::Convert (sigma); //d
        _ppGeometricBrownianMotionProcess = new boost::shared_ptr<QuantLib::GeometricBrownianMotionProcess> (new QuantLib::GeometricBrownianMotionProcess ( _initialValue,  _mue,  _sigma ));
        SetStochasticProcess1D (boost::dynamic_pointer_cast<QuantLib::StochasticProcess1D> (*_ppGeometricBrownianMotionProcess));
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Processes::CGeometricBrownianMotionProcess::CGeometricBrownianMotionProcess (boost::shared_ptr<QuantLib::GeometricBrownianMotionProcess>& childNative, Object^ owner) : CStochasticProcess1D(CGeometricBrownianMotionProcess::typeid)
{
#ifdef HANDLE
	_phGeometricBrownianMotionProcess = NULL;
#endif
	_ppGeometricBrownianMotionProcess = &childNative;
    _ppStochasticProcess1D = new boost::shared_ptr<QuantLib::StochasticProcess1D> (boost::dynamic_pointer_cast<QuantLib::StochasticProcess1D> (*_ppGeometricBrownianMotionProcess));
}
Cephei::QL::Processes::CGeometricBrownianMotionProcess::CGeometricBrownianMotionProcess (QuantLib::GeometricBrownianMotionProcess& childNative, Object^ owner) : CStochasticProcess1D(CGeometricBrownianMotionProcess::typeid)
{
#ifdef HANDLE
	_phGeometricBrownianMotionProcess = NULL;
#endif
	_ppGeometricBrownianMotionProcess = new boost::shared_ptr<QuantLib::GeometricBrownianMotionProcess> (&childNative);
    _ppStochasticProcess1D = new boost::shared_ptr<QuantLib::StochasticProcess1D> (boost::dynamic_pointer_cast<QuantLib::StochasticProcess1D> (*_ppGeometricBrownianMotionProcess));
    _GeometricBrownianMotionProcessOwner = owner;
    _StochasticProcess1DOwner = owner;
}

Cephei::QL::Processes::CGeometricBrownianMotionProcess::CGeometricBrownianMotionProcess (CGeometricBrownianMotionProcess^ copy) : CStochasticProcess1D(CGeometricBrownianMotionProcess::typeid)
{
#ifdef HANDLE
	_phGeometricBrownianMotionProcess = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppGeometricBrownianMotionProcess = new boost::shared_ptr<QuantLib::GeometricBrownianMotionProcess> (copy->GetShared());
        _ppStochasticProcess1D = new boost::shared_ptr<QuantLib::StochasticProcess1D> (boost::dynamic_pointer_cast<QuantLib::StochasticProcess1D> (*_ppGeometricBrownianMotionProcess));
    }
}
Cephei::QL::Processes::CGeometricBrownianMotionProcess::CGeometricBrownianMotionProcess (PLATFORM::Type^ t) : CStochasticProcess1D(CGeometricBrownianMotionProcess::typeid)
{
#ifdef HANDLE
	_phGeometricBrownianMotionProcess = NULL;
#endif
	if (!t->IsSubclassOf(CGeometricBrownianMotionProcess::typeid))
		throw REFNEW Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Processes::CGeometricBrownianMotionProcess::CGeometricBrownianMotionProcess (QuantLib::Handle<QuantLib::GeometricBrownianMotionProcess>& childNative, Object^ owner)  : CStochasticProcess1D(CGeometricBrownianMotionProcess::typeid)
{
	_phGeometricBrownianMotionProcess = &childNative;
	_ppGeometricBrownianMotionProcess = &static_cast<boost::shared_ptr<QuantLib::GeometricBrownianMotionProcess>>(childNative.currentLink());
    _ppStochasticProcess1D = new boost::shared_ptr<QuantLib::StochasticProcess1D> (boost::dynamic_pointer_cast<QuantLib::StochasticProcess1D> (*_ppGeometricBrownianMotionProcess));
    _GeometricBrownianMotionProcessOwner = owner;
}
Cephei::QL::Processes::CGeometricBrownianMotionProcess::CGeometricBrownianMotionProcess (QuantLib::Handle<QuantLib::GeometricBrownianMotionProcess> childNative)  : CStochasticProcess1D(CGeometricBrownianMotionProcess::typeid)
{
	_phGeometricBrownianMotionProcess = &childNative;
	_ppGeometricBrownianMotionProcess = &static_cast<boost::shared_ptr<QuantLib::GeometricBrownianMotionProcess>>(childNative.currentLink());
    _ppStochasticProcess1D = new boost::shared_ptr<QuantLib::StochasticProcess1D> (boost::dynamic_pointer_cast<QuantLib::StochasticProcess1D> (*_ppGeometricBrownianMotionProcess));
}
#endif
#ifdef STRUCT
Cephei::QL::Processes::CGeometricBrownianMotionProcess::CGeometricBrownianMotionProcess (QuantLib::GeometricBrownianMotionProcess childNative)  : CStochasticProcess1D(CGeometricBrownianMotionProcess::typeid)
{
#ifdef HANDLE
	_phGeometricBrownianMotionProcess = NULL;
#endif
	_ppGeometricBrownianMotionProcess = new boost::shared_ptr<QuantLib::GeometricBrownianMotionProcess> (new QuantLib::GeometricBrownianMotionProcess (childNative));
    _ppStochasticProcess1D = new boost::shared_ptr<QuantLib::StochasticProcess1D> (boost::dynamic_pointer_cast<QuantLib::StochasticProcess1D> (*_ppGeometricBrownianMotionProcess));
}
#endif

Cephei::QL::Processes::CGeometricBrownianMotionProcess::~CGeometricBrownianMotionProcess ()
{
    if (_ppGeometricBrownianMotionProcess != NULL)
    {
	    delete _ppGeometricBrownianMotionProcess;
        _ppGeometricBrownianMotionProcess = NULL;
    }
}
Cephei::QL::Processes::CGeometricBrownianMotionProcess::!CGeometricBrownianMotionProcess ()
{
    if (_ppGeometricBrownianMotionProcess != NULL)
    {
	    delete _ppGeometricBrownianMotionProcess;
    }
}
QuantLib::GeometricBrownianMotionProcess& Cephei::QL::Processes::CGeometricBrownianMotionProcess::GetReference ()
{
    if (_ppGeometricBrownianMotionProcess == NULL) throw REFNEW NativeNullException ();
	return **_ppGeometricBrownianMotionProcess;
}
boost::shared_ptr<QuantLib::GeometricBrownianMotionProcess>& Cephei::QL::Processes::CGeometricBrownianMotionProcess::GetShared ()
{
    if (_ppGeometricBrownianMotionProcess == NULL) throw REFNEW NativeNullException ();
	return *_ppGeometricBrownianMotionProcess;
}
QuantLib::GeometricBrownianMotionProcess* Cephei::QL::Processes::CGeometricBrownianMotionProcess::GetPointer ()
{
    if (_ppGeometricBrownianMotionProcess == NULL) throw REFNEW NativeNullException ();
	return &**_ppGeometricBrownianMotionProcess;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::GeometricBrownianMotionProcess>& Cephei::QL::Processes::CGeometricBrownianMotionProcess::GetHandle ()
{
	if (_phGeometricBrownianMotionProcess == NULL)
	{
		_phGeometricBrownianMotionProcess = new Handle<QuantLib::GeometricBrownianMotionProcess> (*_ppGeometricBrownianMotionProcess);
	}
	return *_phGeometricBrownianMotionProcess;
}
#endif
bool Cephei::QL::Processes::CGeometricBrownianMotionProcess::HasNative () 
{
	return (_ppGeometricBrownianMotionProcess != NULL);
}

Double Cephei::QL::Processes::CGeometricBrownianMotionProcess::Diffusion (Double t, Double x)
{
    try
    {
        QuantLib::Time _t = (QuantLib::Time)ValueHelper::Convert (t); //a
        QuantLib::Real _x = (QuantLib::Real)ValueHelper::Convert (x); //a
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppGeometricBrownianMotionProcess)->diffusion ( _t,  _x );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Processes::CGeometricBrownianMotionProcess::Drift (Double t, Double x)
{
    try
    {
        QuantLib::Time _t = (QuantLib::Time)ValueHelper::Convert (t); //a
        QuantLib::Real _x = (QuantLib::Real)ValueHelper::Convert (x); //a
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppGeometricBrownianMotionProcess)->drift ( _t,  _x );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Processes::CGeometricBrownianMotionProcess::X0::get ()
{
    try
    {
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppGeometricBrownianMotionProcess)->x0 ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Processes::IGeometricBrownianMotionProcess^ Cephei::QL::Processes::CGeometricBrownianMotionProcess_Factory::Create (Double initialValue, Double mue, Double sigma)
{
    return REFNEW CGeometricBrownianMotionProcess ( initialValue,  mue,  sigma);
}
